package org.example.aaaatest.a1;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        String[] array1 = {"ac", "ad", "acad", "aga", "agaacad", "sk", "skacad", "hasdjasnskss"};
        System.out.println(findLongestWord(array1)); // 示例1的输出应为"skacad"，但原题示例可能有误

        String[] array2 = {"ac", "ader", "acad", "aga", "agaacad", "skd", "skdacad", "skdader" ,"hasdjasnskss"};
        System.out.println(findLongestWord(array2)); // 示例2的输出应为"skdacad"
    }

    public static String findLongestWord(String[] words) {
        List<String> arrayList = new ArrayList<>(Arrays.asList(words));

        // 记录数据的下坐标
        Integer index = -1;

        //获取每一个字符串
        for (int i = 0; i < arrayList.size(); i++) {
            String str = arrayList.get(i);

            //判断是否在集合中
            Boolean isInArray = false;

            //便利每个字符串
            for (int j = 0; j < str.length(); j++) {
                //获取每个字符串中的每一个字符
                String left = str.substring(0, j);
                String right = str.substring(j);
                //判断是否在集合中
                if (arrayList.contains(left) && arrayList.contains(right)) {
                    isInArray = true;

                }
                //有数据 找到是否是最大的
                if(isInArray){
                    if(i>index){
                        //有新的数据 更新
                        index = i;
                    }else {
                        //第一个数据
                        index = i;
                    }
                }


            }
        }

       return  index ==-1? "": arrayList.get(index) ;
    }
}